package com.wecon.springdemo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author Weikang  Wang
 * @program: com.wecon.springdemo.aop
 * @description: 切面类
 * @date 2020/6/23 17:55
 */
@Aspect
@Component
public class MyAspect {

    @Before("myPointcut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("====前置通知====" + joinPoint);
    }

    @AfterReturning(value = "execution(* com.wecon.springdemo.aop.ProductDao.update(..))")
    public void afterReturning() {
        System.out.println("====后置通知====");
    }

    @Around(value = "execution(* com.wecon.springdemo.aop.ProductDao.delete(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("====环绕前通知====");
        // 执行目标方法
        Object proceed = joinPoint.proceed();
        System.out.println("====环绕后通知====");

        return proceed;
    }

    @AfterThrowing(value = "execution(* com.wecon.springdemo.aop.ProductDao.findOne(..))", throwing = "e")
    public void afterThrowing(Throwable e) {
        System.out.println("====异常抛出通知====" + e.getMessage());
    }

    @After(value = "execution(* com.wecon.springdemo.aop.ProductDao.findAll(..))")
    public void after() {
        System.out.println("====最终通知====");
    }

    @Pointcut(value = "execution(* com.wecon.springdemo.aop.ProductDao.save(..))")
    private void myPointcut(){}

}
